<template>
  <div class="search-container">
    <!-- 导航栏 -->
    <NavBar />
    
    <!-- 搜索结果区域 -->
    <div class="search-content">
      <div class="search-header">
        <h1 class="search-title">搜索结果: <span>{{ searchQuery }}</span></h1>
        <div class="search-stats">找到 {{ filteredCourses.length }} 个结果</div>
      </div>
      
      <!-- 筛选区域 -->
      <div class="filter-section">
        <div class="filter-group">
          <div class="filter-label">学科:</div>
          <div class="filter-options">
            <el-tag 
              v-for="subject in subjects" 
              :key="subject.value"
              :type="activeSubject === subject.value ? '' : 'info'"
              effect="plain"
              class="filter-tag"
              @click="activeSubject = subject.value"
            >
              {{ subject.label }}
            </el-tag>
          </div>
        </div>
        
        <div class="filter-group">
          <div class="filter-label">难度:</div>
          <div class="filter-options">
            <el-tag 
              v-for="level in levels" 
              :key="level.value"
              :type="activeLevel === level.value ? '' : 'info'"
              effect="plain"
              class="filter-tag"
              @click="activeLevel = level.value"
            >
              {{ level.label }}
            </el-tag>
          </div>
        </div>
        
        <div class="filter-group">
          <div class="filter-label">排序:</div>
          <div class="filter-options">
            <el-select v-model="sortBy" placeholder="排序方式" style="width: 150px;">
              <el-option label="最新" value="newest" />
              <el-option label="最热" value="popular" />
              <el-option label="评分最高" value="rating" />
            </el-select>
          </div>
        </div>
      </div>
      
      <!-- 搜索结果列表 -->
      <div class="search-results">
        <div 
          v-for="(course, index) in filteredCourses" 
          :key="index" 
          class="course-card"
          @click="navigateToCourse(course)"
        >
          <div class="course-image" :style="{ backgroundImage: `url(${course.image})` }">
            <div class="course-level" :class="course.level">{{ getLevelText(course.level) }}</div>
            <div class="course-tag" v-if="course.tag">{{ course.tag }}</div>
          </div>
          <div class="course-info">
            <h3 class="course-title">{{ course.title }}</h3>
            <p class="course-description">{{ course.description }}</p>
            <div class="course-meta">
              <div class="course-teacher" v-if="course.teacher">
                <el-avatar :size="24" :src="course.teacher.avatar"></el-avatar>
                <span>{{ course.teacher.name }}</span>
              </div>
              <div class="course-stats">
                <span><el-icon><User /></el-icon> {{ course.students }}</span>
                <span><el-icon><Star /></el-icon> {{ course.rating }}</span>
              </div>
            </div>
            <div class="course-bottom">
              <div class="course-price" :class="{ free: course.price === '免费' }">
                {{ course.price }}
              </div>
              <el-button size="small" type="primary">立即学习</el-button>
            </div>
          </div>
        </div>
        
        <!-- 无结果提示 -->
        <div v-if="filteredCourses.length === 0" class="no-results">
          <el-empty description="没有找到符合条件的课程" />
        </div>
      </div>
      
      <!-- 分页 -->
      <div class="pagination-container" v-if="filteredCourses.length > 0">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="filteredCourses.length"
          :page-size="12"
        />
      </div>
    </div>
    
    <!-- 底栏 -->
    <FooterBar />
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { useStore } from 'vuex';
import { User, Star } from '@element-plus/icons-vue';
import NavBar from '@/components/common/NavBar.vue';
import FooterBar from '@/components/common/FooterBar.vue';

const route = useRoute();
const router = useRouter();
const store = useStore();

// 获取搜索查询参数
const searchQuery = computed(() => route.query.q || '');
const activeSubject = ref('all');
const activeLevel = ref('all');
const sortBy = ref('newest');
const currentPage = ref(1);

// 学科选项
const subjects = [
  { value: 'all', label: '全部' },
  { value: 'math', label: '数学' },
  { value: 'physics', label: '物理' },
  { value: 'chemistry', label: '化学' },
  { value: 'biology', label: '生物' },
  { value: 'english', label: '英语' }
];

// 难度选项
const levels = [
  { value: 'all', label: '全部' },
  { value: 'beginner', label: '初级' },
  { value: 'intermediate', label: '中级' },
  { value: 'advanced', label: '高级' }
];

// 从 store 获取课程数据
const allCourses = computed(() => {
  // 这里应该调用搜索API，但现在我们使用本地数据模拟
  return store.state.home.allCourses || [];
});

// 根据搜索条件过滤课程
const filteredCourses = computed(() => {
  if (!searchQuery.value) return [];
  
  return allCourses.value.filter(course => {
    // 搜索关键词匹配
    const searchMatch = 
      course.title.toLowerCase().includes(searchQuery.value.toLowerCase()) || 
      course.description.toLowerCase().includes(searchQuery.value.toLowerCase());
    
    // 学科筛选
    const subjectMatch = activeSubject.value === 'all' || course.subject === activeSubject.value;
    
    // 难度筛选
    const levelMatch = activeLevel.value === 'all' || course.level === activeLevel.value;
    
    return searchMatch && subjectMatch && levelMatch;
  }).sort((a, b) => {
    // 排序
    if (sortBy.value === 'newest') {
      return b.id - a.id; // 假设id越大越新
    } else if (sortBy.value === 'popular') {
      return b.students - a.students;
    } else if (sortBy.value === 'rating') {
      return b.rating - a.rating;
    }
    return 0;
  });
});

// 获取难度文本
const getLevelText = (level) => {
  switch (level) {
    case 'beginner': return '初级';
    case 'intermediate': return '中级';
    case 'advanced': return '高级';
    default: return '所有级别';
  }
};

// 导航到课程详情页
const navigateToCourse = (course) => {
  router.push(`/course/${course.id}`);
};

// 页面加载时执行搜索
onMounted(() => {
  if (searchQuery.value) {
    // 记录搜索历史
    store.dispatch('home/addSearchHistory', searchQuery.value);
  }
});
</script>

<style lang="scss" scoped>
.search-container {
  min-height: 100vh;
  background-color: #f5f7fa;
  display: flex;
  flex-direction: column;
  
  .search-content {
    flex: 1;
    width: 1200px;
    margin: 20px auto;
    
    .search-header {
      margin-bottom: 20px;
      
      .search-title {
        font-size: 24px;
        color: #303133;
        margin: 0 0 10px;
        
        span {
          color: #1e88e5;
          font-size: 24px;
        }
      }
      
      .search-stats {
        color: #606266;
        font-size: 14px;
      }
    }
    
    .filter-section {
      background-color: #fff;
      border-radius: 8px;
      box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
      padding: 20px;
      margin-bottom: 20px;
      
      .filter-group {
        display: flex;
        align-items: center;
        margin-bottom: 15px;
        
        &:last-child {
          margin-bottom: 0;
        }
        
        .filter-label {
          flex: 0 0 60px;
          font-size: 16px;
          color: #303133;
          font-weight: bold;
        }
        
        .filter-options {
          flex: 1;
          display: flex;
          flex-wrap: wrap;
          gap: 10px;
          
          .filter-tag {
            cursor: pointer;
            font-size: 14px;
            padding: 8px 16px;
          }
        }
      }
    }
    
    .search-results {
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      gap: 20px;
      margin-bottom: 30px;
      
      @media (max-width: 1200px) {
        grid-template-columns: repeat(2, 1fr);
      }
      
      @media (max-width: 768px) {
        grid-template-columns: 1fr;
      }
      
      .course-card {
        background-color: #fff;
        border-radius: 8px;
        overflow: hidden;
        box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
        transition: transform 0.3s, box-shadow 0.3s;
        cursor: pointer;
        
        &:hover {
          transform: translateY(-5px);
          box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
        }
        
        .course-image {
          height: 180px;
          background-size: cover;
          background-position: center;
          position: relative;
          
          .course-level {
            position: absolute;
            top: 10px;
            left: 10px;
            padding: 4px 10px;
            border-radius: 4px;
            font-size: 14px;
            background-color: rgba(0, 0, 0, 0.5);
            color: #fff;
            
            &.beginner {
              background-color: rgba(103, 194, 58, 0.8);
            }
            
            &.intermediate {
              background-color: rgba(30, 136, 229, 0.8);
            }
            
            &.advanced {
              background-color: rgba(245, 108, 108, 0.8);
            }
          }
          
          .course-tag {
            position: absolute;
            top: 10px;
            right: 10px;
            background-color: rgba(30, 136, 229, 0.8);
            color: #fff;
            padding: 4px 10px;
            border-radius: 4px;
            font-size: 14px;
          }
        }
        
        .course-info {
          padding: 20px;
          
          .course-title {
            margin: 0 0 10px;
            font-size: 18px;
            color: #303133;
          }
          
          .course-description {
            margin: 0 0 15px;
            color: #606266;
            font-size: 14px;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
            height: 42px;
          }
          
          .course-meta {
            display: flex;
            justify-content: space-between;
            margin-bottom: 15px;
            
            .course-teacher {
              display: flex;
              align-items: center;
              
              span {
                margin-left: 10px;
                color: #606266;
                font-size: 14px;
              }
            }
            
            .course-stats {
              display: flex;
              
              span {
                display: flex;
                align-items: center;
                margin-left: 15px;
                color: #909399;
                font-size: 14px;
                
                .el-icon {
                  margin-right: 5px;
                }
              }
            }
          }
          
          .course-bottom {
            display: flex;
            justify-content: space-between;
            align-items: center;
            
            .course-price {
              font-weight: bold;
              font-size: 16px;
              color: #f56c6c;
              
              &.free {
                color: #67c23a;
              }
            }
          }
        }
      }
      
      .no-results {
        grid-column: 1 / -1;
        padding: 50px 0;
      }
    }
    
    .pagination-container {
      display: flex;
      justify-content: center;
      margin-top: 20px;
    }
  }
}
</style> 